/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controler;

import graph.Edge;
import graph.Graph;
import graph.Node;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import view.NodeItem;

/**
 *
 * @author borysfan
 */
public class NodeItemControler {
    private NodeItem item;
    private Controler controler;
    private Node nodeData;
    private NodeItemControler instance;
    private ArrayList<EdgeItemControler> edgesItems;
    private Color color;



    NodeItemControler(Controler c){
        edgesItems = new ArrayList<EdgeItemControler>();
        controler = c;

        nodeData = new Node();
        item = new NodeItem(this);
        
        instance = this;

        initActions();
    }

    public void initActions(){
        item.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                Point mouse = e.getPoint();
                int x = item.getX();
                int y = item.getY();
                //System.out.println("\t " + mouse.x + "> "+ x + " "+  mouse.x+ " < " +(x+50)+ "  &&" + mouse.y +">"+ y +"&&"+ mouse.y +"<"+ (y+50));
                if(mouse.x > 0 && mouse.x < 50 && mouse.y > 0 && mouse.y < 50)
                showProperties();
            }
        });
    }

     public BufferedImage getBlockImage(){
        File plik = new File("icons/block.png");

         try {
                BufferedImage obrazek = ImageIO.read(plik);
                return obrazek;
                } catch (IOException ex) {
                    System.out.println("Problem z wczytaniem ikony");
                return null;
            }
    }
    public NodeItem getView(){
            return item;
    }

    public Node getModel(){
        return nodeData;
    }

    public void setModel(Node n){
        nodeData = n;
        setPosition(n.getX(), n.getY());
        setText(n.getName());
        setColor(Color.black);
        
    }

    public void setColor(Color c){
        item.setColor(c);
        item.repaint();
    }

    public void setX(int v){
        nodeData.setX(v);
    }

    public void setY(int v){
        nodeData.setY(v);
    }

    public int getX(){
        return nodeData.getX();
    }

    public int getY(){
        return nodeData.getY();
    }

    public NodeItem getNodeItem(){
        return item;
    }
    public void setText(String s) {
        item.setText(s);
        nodeData.setName(s);
    }

    public void saveName(String s){
        this.nodeData.setName(s);
        refresh();
    }
    public void saveLock(boolean lock){
        this.nodeData.setLock(lock);
    }
    public void setPosition(int x, int y) {
        item.setBounds(x,y,item.getWidth(),item.getHeight());
        nodeData.setX(x);
        nodeData.setY(y);
    }

    void removeMouseListener() {
        MouseListener[] mouseListeners = item.getMouseListeners();
        for(int i=0;i<mouseListeners.length;i++){
            item.removeMouseListener(mouseListeners[i]);
        }
    }

    void removeMouseMotions(){
        MouseMotionListener [] mouseMotions = item.getMouseMotionListeners();
        for(int i=0;i<mouseMotions.length;i++){
            item.removeMouseMotionListener(mouseMotions[i]);
        }
    }



    void addMouseMotions() {
       item.addMouseMotions();
       initActions();
    }

    public void setLabel(String s){
        item.setText(s);
    }

    //////////////ACTIONY listenery


    public void showProperties(){
        controler.showNodeProperties(this);
    }

    public void setConnectActions() {
        System.out.println("Ustawiam na rysowanie linii");
        removeMouseMotions();
        item.addMouseListener(new DrawEdgesListener());
    }

     void removeConnectActions() {
        removeMouseListener();
        removeMouseMotions();
        initActions();
    }

    public void addEdge(NodeItemControler endNode) {
        Graph graph = Graph.getInstance();
        Edge e =graph.addEdge(endNode.getModel(), nodeData);
        EdgeItemControler eic = new EdgeItemControler(controler);
        eic.setEdge(e);
        eic.setBeginNode(this);
        eic.setEndNode(endNode);
        edgesItems.add(eic);
        controler.getMainPanel().addEdge(eic);
    }

    public EdgeItemControler addEdgesToEdgesItems(NodeItemControler end){
        for(int i=0;i<edgesItems.size();i++){
            Node b = edgesItems.get(i).getBeginNode().getModel();
            Node e = edgesItems.get(i).getEndNode().getModel();
            System.out.println(end.getModel().getName() + " " + b.getName() + " " + e.getName());
            if(end.getModel().equals(e) || end.getModel().equals(b)){
                return null;
            }   
        }
        for(int i=0;i<end.edgesItems.size();i++){
            Node b = end.edgesItems.get(i).getBeginNode().getModel();
            Node e = end.edgesItems.get(i).getEndNode().getModel();
            System.out.println(end.getModel().getName() + " " + b.getName() + " " + e.getName());
            if(end.getModel().equals(e) || end.getModel().equals(b)){
                return null;
            }
        }
        EdgeItemControler eic = new EdgeItemControler(controler);
        eic.setBeginNode(this);
        eic.setEndNode(end);
        edgesItems.add(eic);
        
        System.out.println("Dodano "+this.getModel().getName()+" "+end.getModel().getName());
        return eic;

    }

    public void refresh() {
        controler.getMainPanel().getView().updateUI();
    }

    public void redNode() {
        controler.getToolboxControler().redNode();
    }

    void setToolTip(String s) {
        item.setToolTipText(s);
    }

    public boolean showIcons() {
        return controler.getShowIcons();
    }

    public boolean showLabels() {
        return controler.getShowLabels();
    }

    public void setLabel() {
        if (controler.getShowLabels()==true)
            item.setText(nodeData.getName());
        else
        item.setText("");
    }

     public void moveLeft(){
        int x = item.getX();
        int y = item.getY();
        x-=40;
        item.setBounds(x, y, item.getWidth(), item.getHeight());
    }

    public void moveUp(){
        int x = item.getX();
        int y = item.getY();
        y-=40;
        item.setBounds(x, y, item.getWidth(), item.getHeight());
    }

    public void moveRight(){
        int x = item.getX();
        int y = item.getY();
        x+=40;
        item.setBounds(x, y, item.getWidth(), item.getHeight());
    }

    public void moveDown(){
        int x = item.getX();
        int y = item.getY();
        y+=40;
        item.setBounds(x, y, item.getWidth(), item.getHeight());
    }



    //////////////Prywatne klasy


    private class DrawEdgesListener implements MouseListener{


        public void mouseClicked(MouseEvent e) {
        }

        public void mousePressed(MouseEvent e) {
            controler.addNodeEdge(instance);
        }

        public void mouseReleased(MouseEvent e) {
        }

        public void mouseEntered(MouseEvent e) {
        }

        public void mouseExited(MouseEvent e) {
        }
    }



}
